﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YuShiManageSystem.COM;
using YuShiManageSystem.DATA;
using YuShiManageSystem.UI.Card;



namespace YuShiManageSystem.UI.Customer
{
    public partial class Customer : Base
    {
        public tb_customer customer { get; set; }

        public Customer()
        {
            InitializeComponent();
        }

        public Customer(Session se)
            : base(se)
        {
            InitializeComponent();
        }

        //保存按钮
        private void save_Click(object sender, EventArgs e)
        {

            bool flag = CustomerReg();
            if (flag)
            {
                int i = CustomerSave();
                MessageBox.Show(i.ToString());
                if (i > 0)
                    YSMessage.Show("用户保存成功！");
                else
                    YSMessage.Error("用户保存失败！");
            }
        }

        //办卡按钮
        private void makeCard_Click(object sender, EventArgs e)
        {
            bool flag = CustomerReg();
            if (flag)
            {
                int i = CustomerSave();
                if (i > 0)
                    new MakeCard(this.Session).ShowDialog();
                else
                    YSMessage.Error("用户保存失败！");
            }
        }

        //关闭按钮
        private void close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //验证
        private bool CustomerReg()
        {
            //验证必填
            if (!(Validator.Required(this.cName.Text.Trim()) && Validator.Required(this.idcard.Text.Trim()) && Validator.Required(this.phone.Text.Trim())))
            {
                YSMessage.Show("用户名、身份证号码和电话号码不能为空！");
                return false;
            }
            //验证身份证号码
            if (!Validator.CheckCid(this.idcard.Text.Trim()))
            {
                YSMessage.Show("身份证号码错误！");
                return false;
            }
            //验证电话
            if (!(Validator.IsValidTel(this.phone.Text.Trim()) || Validator.IsMobile(this.phone.Text.Trim())))
            {
                YSMessage.Show("电话号码错误！");
                return false;
            }
            ////验证邮箱
            //if (!string.IsNullOrEmpty(this.eMail.Text) & !Validator.IsEmail(this.eMail.Text))
            //{
            //    YSMessage.Show("邮箱格式错误！");
            //    return false;
            //}

            customer = this.Session.DataModel.getCustomerByIdCard(this.idcard.Text.Trim());
            if (customer != null)
            {
                YSMessage.Show("用户已存在！");
                return false;
            }
            return true;
        }

        //保存用户
        private int CustomerSave()
        {
            tb_customer customer = new tb_customer();
            customer.cName = this.cName.Text.Trim();
            customer.idcard = this.idcard.Text.Trim();
            customer.phone =this.phone.Text.Trim();
            customer.sex = this.male.Checked ? "男" : "女";
            customer.birthday = null;
            customer.eMail = string.IsNullOrEmpty(this.eMail.Text.Trim()) ? null : this.eMail.Text.Trim();
            customer.remark = string.IsNullOrEmpty(this.remark.Text.Trim()) ? null : this.remark.Text.Trim();
            return this.Session.DataModel.Save(customer);
        }
    }
}
